<template>
  <el-card class="box-card">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="电话" prop="tel">
        <el-input v-model="ruleForm.tel" placeholder="请输入电话"></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="ruleForm.name" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pwd">
        <el-input
          v-model="ruleForm.pwd"
          placeholder="请输入密码"
          show-password
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="pwd2">
        <el-input
          v-model="ruleForm.pwd2"
          placeholder="请再次输入密码"
          show-password
        ></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="mail">
        <el-input v-model="ruleForm.mail" placeholder="请输入邮箱"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio v-model="ruleForm.sex" label="男">男</el-radio>
        <el-radio v-model="ruleForm.sex" label="女">女</el-radio>
      </el-form-item>
      <el-form-item label="头像" prop="img">
        <el-upload
          class="avatar-uploader"
          action="http://localhost:9000/uploadoss"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          name="file"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <!-- <el-form-item label="角色" prop="role">
  <el-select v-model="ruleForm.roleId" placeholder="请选择">
    <el-option
      v-for="item in roles"
      :key="item.roleName"
      :label="item.roleName"
      :value="item.roleId">
    </el-option>
  </el-select>
      </el-form-item> -->

      <el-form-item label="地区" prop="area">
        <el-select v-model="ruleForm.areaId" placeholder="请选择">
          <el-option
            v-for="item in areas"
            :key="item.areaName"
            :label="item.areaName"
            :value="item.areaId"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <div class="but1">
          <el-button type="primary" @click="submitForm()">立即申请</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </div>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
export default {
  data() {
    return {
      roles: [],
      areas: [],

      ruleForm: {},
      imageUrl: "",
      rules: {
        name: [
          { required: true, message: "请输入姓名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        tel: [
          { required: true, message: "请输入电话", trigger: "blur" },
          { min: 11, max: 11, message: "长度为11个字符", trigger: "change" },
        ],
        pwd: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 10, message: "长度为3-10个字符", trigger: "blur" },
        ],
        pwd2: [
          { required: true, message: "请再次输入密码", trigger: "blur" },
          { min: 3, max: 10, message: "长度为3-10个字符", trigger: "blur" },
        ],
        sex: { required: true, message: "请选择性别", trigger: "change" },
        mail:[
      { required: true, message: '请输入邮箱地址', trigger: 'blur' },
      { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
    ]
      },
    };
  },
  methods: {
    submitForm() {
      this.$axios
        .post("user/apply", this.ruleForm, this.imageUrl)
        .then((res) => {
          if (res.data.code == 200) {
            this.$message.success("申请成功！");
            this.$router.push("/");
          } else {
            this.$message.error("申请失败！电话或邮箱已存在");
          }
        });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 查询所有角色
    selRoles() {
      this.$axios.get("role").then((res) => {
        this.roles = res.data.data.roles;
      });
    },
    // 查询所有地区
    selAreas() {
      this.$axios.get("area").then((res) => {
        this.areas = res.data.data.areaList;
      });
    },
    // 头像上传
    handleAvatarSuccess(res,file) {
      this.imageUrl = res.message;
      this.ruleForm.img = res.message;
      alert(this.imageUrl);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
  mounted() {
    this.selRoles();
    this.selAreas();
  },
};
</script>
<style scoped>
.text {
  font-size: 14px;
}

.item {
  padding: 18px 0;
}

.box-card {
  width: 480px;
  margin-left: 400px;
}
.but1 {
  margin-right: 70px;
}

/* 头像上传 */
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>